package com.mall.service.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.commons.common.CommonPage;
import com.mall.model.domain.UmsResource;
import com.mall.model.domain.UmsResourceCategory;
import com.mall.service.authority.mapper.ResourceMapper;
import com.mall.service.authority.service.ResourceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Pan Rundong
 * @Date 2023-03-11 21:57
 * @Desc 后台资源Service实现类
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, UmsResource> implements ResourceService {
    @Autowired
    private ResourceMapper mapper;
    /**
     * 添加后台资源
     *      不重名储存，重名报错
     *
     * @param resource
     * @return java.lang.Boolean
     * @Author Pan Rundong
     */
    @Override
    public Boolean checkAndSave(UmsResource resource) {
        // 1.设置条件查询是否存在同名
        LambdaQueryWrapper<UmsResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsResource::getName, resource.getName());
        UmsResource umsResource = this.getOne(queryWrapper);

        // 2.判断，不存在则保存
        if (umsResource == null) {
            this.save(resource);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改后台资源
     *      先查询表中是否存在该对象，存在则更新
     *
     * @param id
     * @param umsResource
     * @return void
     * @Author Pan Rundong
     */
    @Override
    public Boolean checkAndUpdate(Long id, UmsResource umsResource) {
        UmsResource resource = this.getById(id);
        if (resource != null) {
            this.updateById(umsResource);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 分页查询后台资源
     *
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @return com.mall.commons.common.CommonPage<com.mall.model.domain.UmsResource>
     * @Author Pan Rundong
     */
    @Override
    public CommonPage<UmsResource> findAll(Integer pageNum, Integer pageSize, Long categoryId, String nameKeyword, String urlKeyword) {
        // 1.设置分页条件
        Page<UmsResource> page = new Page<>(pageNum, pageSize);

        // 2.设置查询条件进行模糊查询，有则查，无则不查
        LambdaQueryWrapper<UmsResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != null, UmsResource::getCategoryId, categoryId);
        queryWrapper.like(nameKeyword != null, UmsResource::getName, nameKeyword);
        queryWrapper.like(urlKeyword != null, UmsResource::getUrl, urlKeyword);

        this.page(page, queryWrapper);

        // 3.封装返回page对象
        CommonPage<UmsResource> commonPage = CommonPage.getPage(page);
        return commonPage;
    }
}
